package redisService
import (
	"gopkg.in/redis.v3"
	"fmt"
	"config"
	"strconv"
)
var Client *redis.Client


func init() {
	fmt.Println("redis init")
	db,_:=strconv.ParseInt((&config.Config{}).String("RedisDB"),10,64)
	Client = redis.NewClient(&redis.Options{
//		Addr:     "10.168.155.113:6379",
		Addr:     (&config.Config{}).String("RedisAddr"),
//		Password: "suwei", // no password set
		Password: (&config.Config{}).String("RedisPasswd"), // no password set
		DB:     	db , // use default DB
	})
	pong, err := Client.Ping().Result()
	fmt.Println(pong, err)
	// Output: PONG <nil>
}

func AddAvgtime(meal_id, avgtime, restaurantId string) {
	err := Client.HSet("avgtime:" + restaurantId, meal_id, avgtime).Err()
	if err != nil {
		panic(err)
	}
}
func AddResAvgtime( avgtime, restaurantId string) {
	err := Client.HSet("resavgtime" , restaurantId, avgtime).Err()
	if err != nil {
		panic(err)
	}
}

func GetResAvgtime() (map[string]interface{}, error) {
	str, err := Client.HGetAllMap("resavgtime").Result()
	if err != nil {
		panic(err)
	}
	m := make(map[string]interface{})
	for key, val := range str {
//		_ = key; _ = val;
		m[key] = val
	}
	return m, err
}

func GetAvgtimeByRestId(restaurantId string) (map[string]interface{}, error) {
	str, err := Client.HGetAllMap("avgtime:" + restaurantId).Result()
	if err != nil {
		panic(err)
	}
	m := make(map[string]interface{})
	for key, val := range str {
		_ = key; _ = val;
		m[key] = val
	}
	fmt.Println(str)
	return m, err
}
